<?php

namespace app\roomcall\controller;


use app\common\model\Goods;
use app\common\model\GoodsCategory;
use app\common\model\ProductOrder;
use app\common\model\VipOrder as VipOrderM;
use app\common\service\SendVoiceMsgService;
use think\Db;


class Product extends Home
{
    public $noNeedLogin = [];

    /**
     *  商品分类
     **/
    public function getCateList()
    {
        $store_id = $this->store_id;
        $goodsCateGory = new GoodsCategory();
        $list = $goodsCateGory->getList($store_id);
        $this->result('获取成功', $list, 200);
    }

    /**
     *  商品列表
     **/
    public function getProductList()
    {
        $store_id = $this->store_id;
        $cate_id = $this->request->post('cate_id');
        $keyword = $this->request->post('keyword', '');
        $limit = $this->request->post('limit', 10);
        $goodsM = new Goods();
        $list = $goodsM->productList($store_id, $cate_id, $keyword, $limit);
        $this->result('获取成功', $list, 200);
    }

    /**
     *  商品详情
     **/
    public function getProductInfo()
    {
        $store_id = $this->store_id;
        $id = $this->request->post('id');
        $goodsM = new Goods();
        $list = $goodsM->productInfo($store_id, $id);
        $this->result('获取成功', $list, 200);

    }

    /**
     * 我的商品，项目列表
     */
    public function myProduct()
    {
        $user_id = $this->userInfo['id'];
        $store_id = $this->store_id;
        $limit = $this->request->post('limit', '10');
        $room_id = $this->request->post('room_id');
        $VipOrderM = new VipOrderM();

        $result = $VipOrderM->orderList('2', $user_id, $store_id, '0', $limit, $room_id);
        $this->result('获取成功', $result, 200);

    }

    /**
     * 添加商品到购物车
     */
    public function add_cart()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $validate = new \app\web\validate\Room();
            if (!$validate->scene('cart')->check($post)) {
                $this->result($validate->getError(), '', 204);
            }
            $orderModel = new ProductOrder();
            $post['engineer_id'] = $this->userInfo['id'];
            $order_data = $orderModel->createOrder($post, $this->userInfo);
            if ($order_data['status'] == true) {
                $post['order_id'] = $order_data['order_id'];
                //下单打印小票
                //FeiEPrintService::printPc($post,$this->userInfo);
                $this->result('添加成功', '', 200);
            }
        } else {
            $this->result('添加失败', '', 204);

        }
    }

    /**
     * 获取房间商品单据
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function room_product()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            if (empty($post['room_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $where = [];

            $project_res = [];
            if ($post['type'] == 1 || $post['type'] == 3) {

                //项目
                $project = Db::name('qt_order')
                    ->field('o.id,r.name as room_name,h.name as hand_card_name')
                    ->alias('o')
                    ->leftJoin('qt_room r', 'r.id = o.room_id')
                    ->leftJoin('qt_hand_card h', 'h.id = o.hand_card_id')
                    ->where(['room_id' => $post['room_id'], 'pay_status' => '1'])
                    ->order('o.id desc')
                    ->find();
                $project_res = Db::name('qt_order_project')
                    ->field('id as product_order_id,name as goods_name,old_name as old_goods_name,order_type,created_time,
                engineer_id,start_time,end_time,old_card_id,hand_card_id,old_card_id,old_room_id,is_change,status,is_add,
                created_order_id as create_id,commission,receiver,sale_id,price,pay_status')
                    ->where('order_id', $project['id'])
                    ->where('is_delete',2)
                    ->whereNotIn('status', ['4', '5'])
                    ->whereNotNull('engineer_id')
                    ->where($where)
                    ->select();
                foreach ($project_res as $key => $value) {
                    $project_res[$key]['work_number'] = Db::name('qt_users')->where('id', $value['engineer_id'])->value('work_number');//技师
                    $project_res[$key]['sale_name'] = Db::name('qt_users')->where('id', $value['sale_id'])->value('real_name');//销售人
                    $project_res[$key]['room_name'] = $project['room_name'];
                    $project_res[$key]['hand_card_name'] = Db::name('qt_hand_card')->where('id', $value['hand_card_id'])->value('name');
                    $project_res[$key]['old_card_name'] = Db::name('qt_hand_card')->where('id', $value['old_card_id'])->value('name');
                    $project_res[$key]['old_room'] = Db::name('qt_room')->where('id', $value['old_room_id'])->value('name');//原房间
                    $project_res[$key]['create_name'] = Db::name('qt_users')->where('id', $value['create_id'])->value('real_name');//下单人
                    $project_res[$key]['type'] = '1';//下单人
                    $project_res[$key]['goods_num'] = '1';//数量
                }
            }

            $product_res = [];
            if ($post['type'] == 2 || $post['type'] == 3) {
                //商品
                $goods = Db::name('qt_product_order')
                    ->field('o.id,r.name as room_name,h.name as hand_card_name,engineer_id')
                    ->alias('o')
                    ->leftJoin('qt_room r', 'r.id = o.room_id')
                    ->leftJoin('qt_hand_card h', 'h.id = o.hand_card_id')
                    ->where(['room_id' => $post['room_id']])
                    ->order('o.id desc')
                    ->find();
                $product_res = Db::name('qt_product_order_item')
                    ->field('id as product_order_id,goods_name,old_goods_name,old_goods_name,goods_num,create_time,
                create_id,sale_id,old_room_id,commission,receiver,goods_price as price,business_type,pay_status')
                    ->where('order_id', $goods['id'])
                    ->where(['status' => '1'])
                    ->where($where)
                    ->select();
                foreach ($product_res as $key => $value) {
                    $product_res[$key]['engineer_name'] = '';//提成
                    $product_res[$key]['sale_name'] = Db::name('qt_users')->where('id', $value['sale_id'])->value('real_name');//销售人
                    $product_res[$key]['room_name'] = $goods['room_name'];
                    $product_res[$key]['hand_card_name'] = '';
                    $product_res[$key]['create_name'] = Db::name('qt_users')->where('id', $value['create_id'])->value('real_name');//提成
                    $product_res[$key]['old_card_name'] = '';
                    $product_res[$key]['status'] = 0;
                    $product_res[$key]['is_change'] = '1';
                    $product_res[$key]['is_add_work'] = 0;
                    $product_res[$key]['order_type'] = '';
                    $product_res[$key]['start_time'] = null;
                    $product_res[$key]['end_time'] = null;
                    $product_res[$key]['old_room'] = Db::name('qt_room')->where('id', $value['old_room_id'])->value('name');//原房间
                    $product_res[$key]['type'] = '2';//商品
                }
            }

            $this->result('请求成功', array_merge($project_res,$product_res), 200);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 删除商品
     */
    public function del_goods()
    {
        $userInfo = $this->userInfo;
        $post = $this->request->post();
        if (empty($post['product_order_id'])) {
            $this->result('缺少参数', '', 204);
        }

        $product_order_info = Db::name('qt_product_order_item')->where(['id' => $post['product_order_id']])->find();
        if (empty($product_order_info)) {
            $this->result('当前商品订单不存在', '', 204);
        }
        if ($product_order_info['pay_status'] == '1') {
            $this->result('已结算商品不允许删除', '', 204);
        }

        //写云大屏播放语音文本
        $order_item = Db::name('qt_product_order_item')
            ->alias('oi')
            ->field(['oi.goods_name', 'oi.goods_num', 'oi.goods_total_price', 'po.room_id', 'po.engineer_id', 'r.name as room_name',])
            ->leftJoin('qt_product_order po', 'po.id=oi.order_id')
            ->leftJoin('qt_room r', 'po.room_id=r.id')
            ->where('oi.store_id', $userInfo['store_id'])
            ->where('oi.id', $post['product_order_id'])
            ->where(['oi.status' => '1'])
            ->find();
        if (!empty($order_item)) {
            $text = $order_item['room_name'] . '  ' . $order_item['goods_name'] . ' 退单';
            Db::name('qt_send_voice_msg')->insert(['content' => $text, 'type' => 1, 'create_time' => time(), 'voice_type' => 2]);
        }

        Db::name('qt_product_order_item')->where('id', $post['product_order_id'])->update(['status' => '2']);

        $this->result('删除成功', '', 200);
    }
}
